﻿global using System;
global using System.Text;
global using System.Diagnostics;
global using System.Drawing;
global using System.IO;
global using System.Linq;
global using MiniExcelLibs;
using MiniExcelLibs.OpenXml;
using System.Collections.Generic;

namespace ConsoleApp1
{
    internal static class StaticClass
    {
        private static void NewMethod()
        {
            var comp = new ObjectComparer();
            var l = MiniExcelLibs.MiniExcel.Query<MyClass>(@"C:\Users\54374\Desktop\组合销售订单.xlsx", sheetName: "Sheet3")
            .GroupBy(x => x.订单号)
            .GroupBy(g => g, comp)
            .Select(g => new
            {
                订单号 = string.Join('\\', g.Select(x => x.Key)),
                订单商品 = g.Key.Select(x => x.订单商品),
                订单 = g,
                组合次数 = g.Count()
            })
            .SelectMany(x => x.订单商品, (g, y) => new
            {
                g.订单号,
                订单商品 = y,
                销售数量 = g.订单.Sum(d => d.Where(c => c.订单商品 == y).Sum(c => c.销售量)),
                g.组合次数
            });
            Console.ReadLine();
        }

        public static bool 介于(this DateTime? me, DateTime start, DateTime end)
        {
            return me == null || me >= start && me <= end;
        }

        private static void 分组前三名求和最大值对应()
        {
            var 结果 = MiniExcel.Query(@"C:\Users\Administrator\Desktop\如何求最大的物品.xlsx", true)
                .OrderByDescending(x => x.数量)
                .GroupBy(x => x.物品名)
                .OrderByDescending(g => g.Take(3).Sum(s => (double)s.数量))
                .First().Key;
            //Console.WriteLine(结果);
        }

        private static void NewMethod1()
        {
            //var path = @"C:\Users\枸杞\Desktop\测试数据.xlsx";
            //var config = new OpenXmlConfiguration { EnableSharedStringCache = false };
            //var l = MiniExcel.Query(path, true, configuration: config)
            //    .Where(x => x.科目编码.ToString().StartsWith("1002")
            //                    || x.科目编码.ToString().StartsWith("6001")
            //                    || x.科目编码.ToString().StartsWith("6401")
            //                    || x.科目编码.ToString().StartsWith("6602"))
            //    .GroupBy(x => x.科目编码)
            //    .Select(x => x.Take(4))
            //    .SelectMany(x => x).ToList();
            //MiniExcel.SaveAs(@"C:\Users\枸杞\Desktop\结果.xlsx", l, overwriteFile: true);
        }

        public static void ForEach<T>(this IEnumerable<T> l, Action<T> action)
        {
            for (int i = 0; i < l.Count(); i++)
            {
                action(l.ToArray()[i]);
            }
        }

        public static void ForEach<T>(this IEnumerable<T> l, Action<T, int> action)
        {
            for (int i = 0; i < l.Count(); i++)
            {
                action(l.ToArray()[i], i);
            }
        }

        //private static void 图片白色转透明()
        //{
        //    var files = System.IO.Directory.GetFiles(@"E:\我的坚果云\图标\office365图标库\Large");
        //    int i = 0;
        //    foreach (string f in files)
        //    {
        //        Image image = Image.FromFile(f);
        //        Bitmap bm = new(image);
        //        bm.MakeTransparent(Color.White);
        //        bm.Save(@"C:\Users\枸杞\Desktop\透明图片\" + Path.GetFileName(f));
        //        image.Dispose();
        //        bm.Dispose();
        //        Console.WriteLine(i++.ToString() + f);
        //    }
        //}
    }

    internal class MyClass
    {
        public string 订单号 { get; set; }
        public string 订单商品 { get; set; }
        public int 销售量 { get; set; }
    }

    internal class ObjectComparer : IEqualityComparer<IGrouping<string, MyClass>>
    {
        public bool Equals(IGrouping<string, MyClass> x, IGrouping<string, MyClass> y)
        {
            return x.All(x1 => y.Any(y1 => y1.订单商品 == x1.订单商品)) && y.All(y1 => x.Any(x1 => x1.订单商品 == y1.订单商品));
        }

        public int GetHashCode(IGrouping<string, MyClass> obj)
        {
            return obj.Sum(x => x.订单商品.GetHashCode());
        }
    }
}